<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 主体内容部分区域 -->
    <el-card class="box-card">
      <el-input
        placeholder="请输入内容"
        v-model="inputvalue"
        clearable
        class="input-with-select"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="searchInfo"
        ></el-button>
      </el-input>
      <el-button type="primary" @click="goAddPage">添加商品</el-button>

      <!-- 表单区域 -->
      <el-table :data="cateList" style="width: 100%" stripe border>
        <el-table-column label="#" type="index" width="50"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="600">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="100">
        </el-table-column>
        <el-table-column prop="goods_number" label="商品数量" width="80">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="130"> </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="editItem(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="deleteList(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryObj.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      inputvalue: "",
      total: 0,
      queryObj: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      cateList: [],
    };
  },
  created() {
    this.getCateList();
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryObj,
      });
      if (res.meta.status !== 200) {
        return this.$message.error("获取列表失败");
      }
      this.$message.success("获取列表成功");
      this.cateList = res.data.goods;
      this.total = res.data.total;
    },
    //分页区域部分发生了改变
    handleSizeChange(val) {
      this.queryObj.pagesize = val;
      this.getCateList();
    },
    //修改当前所处的页数
    handleCurrentChange(val) {
      this.queryObj.pagenum = val;
      this.getCateList();
    },
    //根据关键字搜索
    searchInfo() {
      //根据搜索的关键字发送请求获得数据
      this.queryObj.query = this.inputvalue;
      this.getCateList();
    },
    //根据 ID 删除信息
    async deleteList(role) {
      const result = await this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).catch(err => err)
      if(result !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$http.delete(`goods/${role.goods_id}`)
      if (res.meta.status !== 200) return this.$message.error("删除失败");
      this.$message.success("删除成功");
    },
    goAddPage() {
      this.$router.push('/goods/add')
    }
  },
};
</script>

<style lang="less" scoped>
.input-with-select {
  width: 400px;
  margin-right: 30px;
}
</style>